package com.ywl.gulimall.coupon.service.impl;

import com.ywl.common.dto.SkuReductionDto;
import com.ywl.gulimall.coupon.entity.MemberPriceEntity;
import com.ywl.gulimall.coupon.entity.SkuLadderEntity;
import com.ywl.gulimall.coupon.service.MemberPriceService;
import com.ywl.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywl.common.utils.PageUtils;
import com.ywl.common.utils.Query;

import com.ywl.gulimall.coupon.dao.SkuFullReductionDao;
import com.ywl.gulimall.coupon.entity.SkuFullReductionEntity;
import com.ywl.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveSkuReduction(SkuReductionDto skuReductionDto) {
        // sms_sku_ladder
        if (skuReductionDto.getFullCount() > 0 || skuReductionDto.getDiscount().compareTo(new BigDecimal(0)) == 1) {
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReductionDto, skuLadderEntity);
            skuLadderEntity.setAddOther(skuReductionDto.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        // sms_sku_full_reduction
        if (skuReductionDto.getFullPrice().compareTo(new BigDecimal(0)) == 1
                || skuReductionDto.getReducePrice().compareTo(new BigDecimal(0)) == 1) {
            SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionDto, skuFullReduction);
            skuFullReduction.setAddOther(skuReductionDto.getPriceStatus());
            this.save(skuFullReduction);
        }

        // sms_member_price
        List<MemberPriceEntity> collect = skuReductionDto.getMemberPrice().stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setSkuId(skuReductionDto.getSkuId());
            return memberPriceEntity;
        }).filter(item -> {
            return item.getMemberPrice().compareTo(new BigDecimal(0)) == 1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);

    }

}